/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import POJO.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Master
 */
public class DAO_KhachHang {
    public List<POJO_KhachHang> LoadList() throws SQLException{
        DataProvider data = new DataProvider();
        data.Open();
        
        List<POJO_KhachHang> list = new ArrayList<>();
        String query = "select * from khach_hang";
        ResultSet rs = data.ExecuteQuery(query);
        
        while(rs.next()){
            POJO_KhachHang khachHang = new POJO_KhachHang();
            
            khachHang.setCMND(rs.getString("CMND"));
            khachHang.setDIACHI(rs.getString("DiaChi"));
            khachHang.setHOTEN(rs.getString("HoTen"));
            khachHang.setNGAYSINH(rs.getDate("NgaySinh"));
            
            list.add(khachHang);
        }
        return list; 
    }
    
    public void Create(POJO_KhachHang khachHang){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("INSERT INTO `khach_hang`(`CMND`, `HoTen`, `DiaChi`, `NgaySinh`) VALUES ('%s','%s','%s','%s')", khachHang.getCMND(), khachHang.getHOTEN(), khachHang.getDIACHI(), khachHang.getNGAYSINH());
        
        data.ExeCuteUpdate(query);
    }
    
    public void Remove(POJO_KhachHang khachHang){
        DataProvider data = new DataProvider();
        data.Open();

        String query = String.format("DELETE FROM `khach_hang` WHERE cmnd = '%s'", khachHang.getCMND());

        data.ExeCuteUpdate(query);
    }
    
    public void Edit(POJO_KhachHang khachHang){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("UPDATE `khach_hang` SET `CMND`='%s',`HoTen`='%s',`DiaChi`='%s',`NgaySinh`='%s' WHERE CMND = '%s'", khachHang.getCMND(), khachHang.getHOTEN(), khachHang.getDIACHI(), khachHang.getNGAYSINH(), khachHang.getCMND());
        
        data.ExeCuteUpdate(query);
    }
}
